package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.KnowledgePoint;
import com.google.common.collect.Lists;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * 知识点Dao类
 * Created by chenxin on 2014/12/8.
 */
@Repository
public class KpDao extends BaseDao {
    /**
     * 根据名称获取知识点
     * @param kp 知识点
     * @return KnowledgePoint
     *
     * @author cx
     * @since 2014-12-09
     */
    public boolean isExist(KnowledgePoint kp) {
        String sql = "SELECT id FROM t_knowledge_point WHERE UPPER(sName)=:kpName LIMIT 1";

        try {
            Long id = namedParameterJdbcTemplate.queryForObject(sql, new MapSqlParameterSource().addValue("kpName", kp.getName().toUpperCase()), Long.class);
            kp.setId(id);

            log.debug("根据名称获取知识点成功");
            return true;
        } catch (EmptyResultDataAccessException ex) {
            log.debug("根据名称获取知识点失败");
            return false;
        }
    }

    /**
     * 保存知识点
     * @param kp 知识点
     * @return int
     *
     * @author cx
     * @since 2014-12-8
     */
    public boolean save(KnowledgePoint kp) {
        String sql = "INSERT INTO t_knowledge_point VALUES(NULL,:kpName)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        MapSqlParameterSource sps = new MapSqlParameterSource().addValue("kpName", kp.getName());

        int result = namedParameterJdbcTemplate.update(sql, sps, keyHolder);
        if (result == 1) {
            log.debug("获取新增后的知识点ID成功:result == 1");
            //获取新增后的知识点ID
            kp.setId(keyHolder.getKey().longValue());

            return true;
        }

        log.debug("保存知识点失败");
        log.warn("保存知识点失败:result->{}", result);
        return false;
    }

    /**
     * 删除知识点
     * @param kpId 知识点ID
     * @return boolean
     *
     * @author cx
     * @since 2014-12-09
     */
    public boolean delete(final long kpId) {
        String sql = "DELETE FROM t_knowledge_point WHERE id=:kpId";

        return namedParameterJdbcTemplate.update(sql, new MapSqlParameterSource().addValue("kpId", kpId)) == 1;
    }

    /**
     * 获取指定的知识点集合
     * @param kpIds 知识点ID集合
     * @return List
     *
     * @author cx
     * @since 2014-12-09
     */
    public List<KnowledgePoint> getAll(final Collection<Long> kpIds) {
        if (kpIds == null || kpIds.isEmpty()) {
            log.debug("知识点集合点为空：kpIds == null || kpIds.isEmpty()");
            return Lists.newArrayList();
        }

        String sql = "SELECT * FROM t_knowledge_point WHERE id IN(:kpIdSet)";

        log.debug("获取指定的知识点集合");
        return namedParameterJdbcTemplate.query(sql, new MapSqlParameterSource().addValue("kpIdSet", kpIds), new RowMapper<KnowledgePoint>() {
            @Override
            public KnowledgePoint mapRow(ResultSet rs, int rowNum) throws SQLException {
                KnowledgePoint entity = new KnowledgePoint();

                entity.setId(rs.getLong("id"));
                entity.setName(rs.getString("sName"));

                return entity;
            }
        });
    }

    /**
     * 根据搜索关键字获取知识点
     * @param keyword 搜索关键字
     * @return List
     *
     * @author cx
     * @since 2014-12-09
     */
    public List<KnowledgePoint> getKpsBySearch(final String keyword, final int top) {
        String sql = "SELECT * FROM t_knowledge_point WHERE UPPER(sName) LIKE :keyword LIMIT :top";
        SqlParameterSource sps = new MapSqlParameterSource().addValue("keyword", "%" + keyword.toUpperCase() + "%").addValue("top", top);

        log.debug("根据搜索关键字获取知识点");
        return namedParameterJdbcTemplate.query(sql, sps, new RowMapper<KnowledgePoint>() {
            @Override
            public KnowledgePoint mapRow(ResultSet rs, int rowNum) throws SQLException {
                KnowledgePoint entity = new KnowledgePoint();

                entity.setId(rs.getLong("id"));
                entity.setName(rs.getString("sName"));

                return entity;
            }
        });
    }

}
